package groovy;

import org.stringtemplate.v4.*;

import java.util.Locale;

public class TestStringTemplate {
    public static void main(String[] args) {
        String jsonTemplate = "{\"inpNo\":\"$inpNo\",\"name\":\"$name\",\"birthday\":\"$birthday\",\"sex\":\"<%String.valueOf(sex) %>\",\"odsOperate\":[<% for (item in odsOperate) { %>    {        \"icdCode\":\"$item.icdCode\",        \"surgeryCode\":\"$item.surgery_code\"    },<% } %>]<% for (item in odsZd) { %>    {        \"icdCode\":\"$item.icdCode\",        \"surgeryCode\":\"$item.surgery_code\"    },<% } %>}";
        STGroup g = new STGroupString(jsonTemplate);
        g.registerRenderer(String.class, new StringRenderer());
        String jsonSource = "{\"inpNo\":\"001\",\"name\":\"张三\",\"birthday\":\"2021-03-11\",\"sex\":0,\"odsOperate\":[{\"icdCode\":\"7777\",\"surgery_code\":\"6666\"},{\"icdCode\":\"2222\",\"surgery_code\":\"333\"}],\"odsZd\":[]}";
    }
    private void test1(){
        String template =
                "foo(x,y) ::= << <x; format=\"%,d\"> <y; format=\"%,2.3f\"> >>\n";
        STGroup g = new STGroupString(template);
        g.registerRenderer(Number.class, new NumberRenderer());
        ST st = g.getInstanceOf("foo");
        st.add("x", -2100);
        st.add("y", 3.14159);
        String result = st.render(new Locale("pl"));
        System.out.println(result);
    }
}
